/**
 @file Reply_Handler.h

 Handles replying messages reacted by multi-cast messages in MDNS

 @author Kyoungho An <kyoungho.an@gmail.com>
*/

#ifndef _REPLY_HANDLER_H_
#define _REPLY_HANDLER_H_

#include "ace/Reactor.h"
#include "ace/SOCK_Dgram_Mcast.h"
#include "ace/INET_Addr.h"
#include "ace/Log_Msg.h"
#include "ace/Event_Handler.h"

extern "C" {
#include "mdnsd.h"
#include "mdns.h"
}

#define MDNS_ADDR "224.0.0.251"
#define MDNS_PORT 5353

#define BUFFER_SIZE 65536

namespace MDNS {
class Reply_Handler : public ACE_Event_Handler {
public:
	// constructor
	Reply_Handler (ACE_Reactor *r = ACE_Reactor::instance ());

	// destructor
	~Reply_Handler (void) {}

	// open multicast socekt and register the handler to the reactor
	int open (struct mdnsd *svr);

	// handle incoming events
	// the events we are interested in are the incoming multicast data
	virtual int handle_input (ACE_HANDLE fd = ACE_INVALID_HANDLE);

	// handle clean up events
	virtual int handle_close (ACE_HANDLE fd = ACE_INVALID_HANDLE);

	// return socket's IO handle 
	virtual ACE_HANDLE get_handle (void) const
		{ return this->mcast_dgram_.get_handle (); }

private:
	// multicast socket
	ACE_SOCK_Dgram_Mcast mcast_dgram_; 
	// multicast address
	ACE_INET_Addr mcast_addr_;
	// MDNS daemon object
	struct mdnsd *svr_;
	// replying packet
	struct mdns_pkt *mdns_reply_;
	// socket buffer
	ACE_UINT8 pkt_buf_[BUFFER_SIZE];
};
}

#endif
